
var cluster = require('cluster')

function startWork(){
    var worker = cluster.fork()
    console.log('CLUSTER Worker %d started',worker.id);
}


if(cluster.isMaster){

    require('os').cpus().forEach(function(){
        startWork();
    });


    // 记录所有断开的 工作线程
    cluster.on('disconnect',function(worker){
        console.log('CLUSTER Worker %d disconnect from cluster.',worker.id);
    
    });

    // 当有 工作线程 退出时， 创建一个新的工作线程 代替他
    cluster.on('exit',function(worker,code,signal){
        console.log('CLUSTER Worker %d died with exit code %d (%s)',worker.id,code,signal);

        startWork();
    })


}else{

    //在当前工作线程上， 启动httpServer 服务器
    require('./httpServer')();
}